package com.jinzhi.jzweb.controller;


import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.ReserveSpecificationInventoryDO;
import com.jinzhi.jzweb.service.ReserveSpecificationInventoryService;
import com.jinzhi.jzweb.utils.IDUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.ReserveStoreCommodityDO;
import com.jinzhi.jzweb.service.ReserveStoreCommodityService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 教程预定商城货品表
 * </pre>
 * <small> 2023-08-17 10:49:39 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/reserveStoreCommodity")
public class ReserveStoreCommodityController extends BaseController {

	@Autowired
	private ReserveStoreCommodityService reserveStoreCommodityService;

	@Autowired
	private ReserveSpecificationInventoryService reserveSpecificationInventoryService;

	@GetMapping()
	@RequiresPermissions("jzweb:reserveStoreCommodity:reserveStoreCommodity")
	String ReserveStoreCommodity(){
		return "jzweb/reserveStoreCommodity/reserveStoreCommodity";
	}


	@ResponseBody
	@GetMapping("/list")
	public Result<Page<ReserveStoreCommodityDO>> list(ReserveStoreCommodityDO reserveStoreCommodityDO){

		EntityWrapper entityWrapper =new EntityWrapper();
		if (!"".equals(reserveStoreCommodityDO.getType()) && reserveStoreCommodityDO.getType() !=null){
			entityWrapper.eq("type",reserveStoreCommodityDO.getType());
		}
		if (!"".equals(reserveStoreCommodityDO.getCommodityName()) && reserveStoreCommodityDO.getCommodityName() !=null){
			entityWrapper.eq("commodity_name",reserveStoreCommodityDO.getCommodityName());
		}
		entityWrapper.orderBy("create_time",false);

		// 查询列表数据
		Page<ReserveStoreCommodityDO> page = reserveStoreCommodityService.selectPage(getPage(ReserveStoreCommodityDO.class),
				entityWrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ReserveStoreCommodityDO obj = (ReserveStoreCommodityDO) page.getRecords().get(i);
			List<String> intList = splitStrArrayList(obj.getSpecificationId());
			obj.setReservelSpecificationInventories((List<ReserveSpecificationInventoryDO>) reserveSpecificationInventoryService.selectBatchIds(intList));
			list.add(obj);
		}
		page.setRecords(list);
		return Result.ok(page);
	}

	private static List<String> splitStrArrayList(String str) {
		List<String> stringList = new ArrayList<>();
		if (str != null) {
			String[] strs = str.split(",");
			stringList.addAll(Arrays.asList(strs));
			return stringList;
		}
		return null;
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:reserveStoreCommodity:add")
	String add(){
		return "jzweb/reserveStoreCommodity/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:reserveStoreCommodity:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ReserveStoreCommodityDO reserveStoreCommodity = reserveStoreCommodityService.selectById(id);
		model.addAttribute("reserveStoreCommodity", reserveStoreCommodity);
		return "jzweb/reserveStoreCommodity/edit";
	}


	@Log("删除教程预定商城货品表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:reserveStoreCommodity:remove")
	public Result<String>  remove( Long id){
		reserveStoreCommodityService.deleteById(id);
		return Result.ok();
	}

	@Log("批量删除教程预定商城货品表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:reserveStoreCommodity:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		reserveStoreCommodityService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}





	@Log("添加积分商城货品表")
	@ResponseBody
	@PostMapping("/save")
	@Transactional
	@RequiresPermissions("jzweb:reserveStoreCommodity:add")
	public Result<String> save( ReserveStoreCommodityDO reserveStoreCommodityDO){
		String specificationId = "";
		reserveStoreCommodityDO.setState(1); //默认已上架
		reserveStoreCommodityDO.setCreateTime(new Date());//创建时间

		String[] array = splitStrArray(reserveStoreCommodityDO.getSpecificationName());
		List<ReserveSpecificationInventoryDO> list =new ArrayList<>();
		ReserveSpecificationInventoryDO reserveSpecificationInventoryDO = new ReserveSpecificationInventoryDO();;
		for (int i=0;i<array.length;i++){
			if (i%2==0){ //偶数
				reserveSpecificationInventoryDO.setSpecification(array[i]);
			}else{
				reserveSpecificationInventoryDO.setSpecificationQuantity(Integer.valueOf(array[i]));
			}
			if (i%2!=0) {
				list.add(reserveSpecificationInventoryDO);
			}
			if (i%2!=0) {
				reserveSpecificationInventoryDO = new ReserveSpecificationInventoryDO();
			}
		}
		for (int i=0;i<list.size();i++){
			String id = getId(); //生成id
			if (StringUtil.isNull(specificationId)){
				specificationId = id;
			}else {
				specificationId = specificationId+","+id;
			}
			list.get(i).setId(Long.valueOf(id));
		}

		reserveStoreCommodityDO.setSpecificationId(specificationId);
		reserveSpecificationInventoryService.insertBatch(list);


		reserveStoreCommodityService.insert(reserveStoreCommodityDO);

		return Result.ok();
	}


	//生成一个随机id
	private String getId(){
		IDUtil idUtil = new IDUtil("yyyyMMddmmssSSS");//yyyy-MM-dd HH:mm:ss：SSS
		return idUtil.generatedNoByFormatDateAndRandom(3,"3");
	}


	public static String[] splitStrArray(String str) {
		if (str != null) {
			return str.split(",");
		}
		return null;
	}

	@Log("修改积分商城货品表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:reserveStoreCommodity:edit")
	public Result<String>  update( ReserveStoreCommodityDO reserveStoreCommodityDO){
		ReserveStoreCommodityDO reserveStoreCommodity = reserveStoreCommodityService.selectById(reserveStoreCommodityDO.getId());
		reserveStoreCommodity.setType(reserveStoreCommodityDO.getType());
		reserveStoreCommodity.setCommodityName(reserveStoreCommodityDO.getCommodityName());
		reserveStoreCommodity.setCommodityPrices(reserveStoreCommodityDO.getCommodityPrices());
		reserveStoreCommodity.setCommodityDescribe(reserveStoreCommodityDO.getCommodityDescribe());
		reserveStoreCommodity.setSpecificationId(reserveStoreCommodityDO.getSpecificationId());
		reserveStoreCommodity.setInventoryQuantity(reserveStoreCommodityDO.getInventoryQuantity());
		reserveStoreCommodity.setParameter(reserveStoreCommodityDO.getParameter());
		reserveStoreCommodity.setCommodityImg(reserveStoreCommodityDO.getCommodityImg());
		reserveStoreCommodity.setThreshold(reserveStoreCommodityDO.getThreshold());
		reserveStoreCommodity.setCreateTime(reserveStoreCommodityDO.getCreateTime());
		reserveStoreCommodity.setUpdateTime(reserveStoreCommodityDO.getUpdateTime());
		reserveStoreCommodity.setCommodityParticularsImg(reserveStoreCommodityDO.getCommodityParticularsImg());
		reserveStoreCommodity.setSort(reserveStoreCommodityDO.getSort());
		reserveStoreCommodity.setState(reserveStoreCommodityDO.getState());
		reserveStoreCommodity.setSpecificationName(reserveStoreCommodityDO.getSpecificationName());
		boolean update = reserveStoreCommodityService.updateById(reserveStoreCommodity);
		return update ? Result.ok() : Result.fail();
	}

}
